<template>
  <div class="user-info">
    <el-row :gutter="20" align="middle">
      <el-col :span="24" :md="4" class="avatar-container">
        <div class="avatar">
          <el-avatar
            :src="user.userphoto"
            size="small"
            style="width: 150px; height: 150px"
          ></el-avatar>
        </div>
      </el-col>
      <el-col :span="24" :md="20">
        <div class="info">
          <el-row :gutter="20">
            <el-col :span="12" class="item">
              <span class="label">用户名</span>
              <span class="value">{{ user.account }}</span>
            </el-col>
            <el-col :span="12" class="item">
              <span class="label">性别</span>
              <span class="value">
                <span v-if="user.sex == 1">男</span>
                <span v-else>女</span>
              </span>
            </el-col>
            <el-col :span="12" class="item">
              <span class="label">出生日期</span>
              <span class="value">{{ user.birth }}</span>
            </el-col>
            <el-col :span="12" class="item">
              <span class="label">手机号码</span>
              <span class="value">{{ user.phone }}</span>
            </el-col>
            <el-col :span="24">
              <el-divider></el-divider>
            </el-col>
            <el-col :span="12" class="item">
              <span class="label">地址</span>
              <span class="value">{{ user.address }}</span>
            </el-col>
            <el-col :span="12" class="item">
              <span class="label">邮箱地址</span>
              <span class="value">{{ user.email }}</span>
            </el-col>
            <el-col :span="12" class="item">
              <el-button @click="updatepasswordopen" size="large"
                >修改密码</el-button
              >
            </el-col>
          </el-row>
          <el-divider></el-divider>
          <el-row :gutter="20">
            <el-col :span="24" class="pet-title">
              <span class="label"
                ><i class="el-icon-paw icon"></i> 我的宠物</span
              >
            </el-col>
            <template v-for="pet in pets" :key="pet">
              <el-col :span="8" class="pet-item">
                <div class="pet-container">
                  <div class="avatar">
                    <el-avatar :src="pet.avatar" size="small"></el-avatar>
                  </div>
                  <div class="info">
                    <div class="item">
                      <span class="label">昵称：</span>
                      <span class="value">{{ pet.nickname }}</span>
                    </div>
                    <div class="item">
                      <span class="label">品种：</span>
                      <span class="value">{{ pet.breed }}</span>
                    </div>
                    <div class="item">
                      <span class="label">性别：</span>
                      <span class="value">{{ pet.gender }}</span>
                    </div>
                    <div class="item">
                      <span class="label">年龄：</span>
                      <span class="value">{{ pet.age }} 岁</span>
                    </div>
                  </div>
                </div>
              </el-col>
            </template>
          </el-row>
        </div>
      </el-col>
    </el-row>
  </div>
  <!-- 修改密码区域 -->
  <el-dialog v-model="dialogVisible" title="修改密码" width="50%">
    <el-form label-width="100px" size="small">
      <el-form-item label="旧密码">
        <el-input
          size="small"
          v-model="form.oldpassword"
          type="password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码">
        <el-input
          size="small"
          v-model="form.newpassword"
          type="password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认新密码">
        <el-input
          size="small"
          v-model="form.nowpassword"
          type="password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updatepassword">修 改</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
export default {
  name: "PersionView",
  data() {
    return {
      user: {},
      pets: [
        {
          nickname: "Tiger",
          breed: "孟买猫",
          gender: "Male",
          age: 3,
          avatar: "",
        },
        {
          nickname: "Charlie",
          breed: "狸花猫",
          gender: "Female",
          age: 2,
          avatar: "",
        },
      ],
      dialogVisible: false,
      form: {
        oldpassword: "",
        newpassword: "",
        nowpassword: "",
      },
    };
  },
  created() {
    this.user = JSON.parse(window.sessionStorage.getItem("user"));
  },
  methods: {
    //修改密码打开方法
    updatepasswordopen() {
      this.form = {
        oldpassword: "",
        newpassword: "",
        nowpassword: "",
      };
      this.dialogVisible = true;
    },
    updatepassword() {
      const utilMd5 = require("../../components/utils/md5");
      if (utilMd5.hexMD5(this.form.oldpassword) != this.user.password) {
        return this.$message.error("旧密码输入错误!");
      } else if (
        utilMd5.hexMD5(this.form.newpassword) !=
        utilMd5.hexMD5(this.form.nowpassword)
      ) {
        return this.$message.error("两次密码输入不一致!");
      } else {
        this.form.newpassword = utilMd5.hexMD5(this.form.newpassword);
        this.$http
          .post(
            "/user/updatepassword?id=" +
              this.user.id +
              "&newpassword=" +
              this.form.newpassword
          )
          .then((res) => {
            if (res.data.status == 200) {
                console.log(res);
              this.$message.success("修改密码成功!");
              //清空token
              window.sessionStorage.clear();
              //跳转到登录页面
              this.$router.push({
                name: "login",
              });
            } else {
                console.log(res);
              this.$message.error("修改密码失败!");
            }
          });
      }
    },
  },
};
</script>

<style>
.user-info {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  margin: 0 auto;
  max-width: 1000px;
}

.avatar-container {
  text-align: center;
  margin-bottom: 20px;
}

.avatar {
  display: inline-block;
}

.info {
  padding: 20px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1);
}

.label {
  font-weight: bold;
  margin-right: 10px;
}

.icon {
  font-size: 18px;
  margin-right: 7px;
}

.item {
  margin-bottom: 10px;
}

.pet-title {
  margin-bottom: 20px;
}

.pet-item {
  margin-bottom: 10px;
}

.pet-container {
  display: flex;
  align-items: center;
  background-color: #f5f5f7;
  padding: 10px;
  border-radius: 5px;
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}

.pet-container .avatar {
  margin-right: 20px;
}
</style>